package com.gjh.study.basic.thread;

import java.util.concurrent.*;

/**
 * @author guangjh
 * @version 1.0
 * @date 2020/7/7 16:19
 */
public class ThreadPoolExecutorDemo {
    // 核心线程池数量
    private static final int CORE_POOL_SIZE = 5;
    // 最大线程池数量
    private static final int MAX_POOL_SIZE = 10;
    // 等待队列数量
    private static final int QUEUE_CAPACITY = 2;
    private static final Long KEEP_ALIVE_TIME = 1L;

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(QUEUE_CAPACITY), new ThreadPoolExecutor.CallerRunsPolicy());
        for (int i = 0; i < 10; i++) {
            Runnable worker = new MyRunnable("" + i);
            executor.execute(worker);
        }

        Future task;
        for (int i = 0; i < 5; i++) {
            task = executor.submit(new CallableImpl());
            System.out.println("线程返回结果：" + task.get());
        }

        executor.shutdown();
        while (!executor.isTerminated()) {}
        System.out.println("Finished all threads.");
    }
}
